package com.glsc.ngateway.opmanage.controller.performance;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.performance.DayPerformanceItem;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.DayPerformanceDto;
import com.glsc.ngateway.opmanage.service.performance.DayPerformanceItemService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *  绩效管理- 每日积分卡
 */
@RestController
@RequestMapping("/api/performance/item")
@Slf4j
public class DayPerformanceItemController {

    @Resource
    private UserCache userCache;

    @Resource
    private DayPerformanceItemService dayPerformanceItemService;



    /**
     * 分页查询
     *
     * @param obj
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<DayPerformanceItem>> page(@RequestBody DayPerformanceDto obj) {

        Map<String, Object> param = new HashMap<>();

        if (StringUtils.isNotBlank(obj.getUserName())) {
            param.put("LIKE_userName", obj.getUserName());
        }
        if (StringUtils.isNotBlank(obj.getPostName())) {
            param.put("LIKE_postName", obj.getPostName());
        }

        Page<DayPerformanceItem> page = dayPerformanceItemService.page(PageUtil.initPageNo(obj.getPageNo()), PageUtil.initPageSize(obj.getPageSize()), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据di查询
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<DayPerformanceItem> findById(@ApiParam(value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(dayPerformanceItemService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<DayPerformanceItem> save(@RequestBody DayPerformanceItem obj, HttpServletRequest request) {
        User user = userCache.get(request);
        obj.setUserName(user.getName());
        obj.setUserId(user.getId().intValue());
        obj.setUserOaAccount(user.getUsername());
        DayPerformanceItem save = dayPerformanceItemService.save(obj);
        return PlatformResponse.successData(save);
    }




}
